package leetcode_core.leetcode_3;

import java.util.LinkedList;
import java.util.List;

public class PreorderTraversal {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new LinkedList<>();
        traverse(root,list);
        return list;
    }

    private void traverse(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }
        //前序遍历
        list.add(root.val);
        traverse(root.left,list);
        traverse(root.right,list);
    }


}
